﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MebApp.Launcher
{
    public partial class BaseForm : Form
    {
        public BaseForm()
        {
            InitializeComponent();
            if (this.Owner != null)
                this.StartPosition = FormStartPosition.CenterParent;
            else
                this.StartPosition = FormStartPosition.CenterScreen;
        }

        public T ReadModel<T>()
        {
            var fileName = Path.Combine(Application.StartupPath, typeof(T).Name + ".json");
            if (!File.Exists(fileName))
            {
                return default(T);
            }
            else
            {
                try
                {
                    return JsonConvert.DeserializeObject<T>(File.ReadAllText(fileName));
                }
                catch
                {
                    return default(T);
                }
            }
        }

        public void WriteModel<T>(T value)
        {
            var fileName = Path.Combine(Application.StartupPath, typeof(T).Name + ".json");
            var text = JsonConvert.SerializeObject(value);
            File.WriteAllText(fileName, text);
        }
    }
}
